<template>
 <div>
<el-dialog
  :title="title"
  :visible.sync="dialogVisible"
  width="20%"
>
  <el-form ref="form" :model="form" label-width="80px" :rules="rules">
    <el-form-item label="所属学科" prop="subjectID">
      <el-select  placeholder="请选择" v-model="form.subjectID" >
          <el-option v-for="item in subjectList" :key="item.value" :value="item.value" :label="item.label">
      </el-option>
    </el-select>
  </el-form-item>
  <el-form-item label="目录名称" prop="directoryName" v-if="flag">
<el-input v-model="form.directoryName" placeholder="请输入内容"  ></el-input>
  </el-form-item>
    <el-form-item label="标签名称" prop="tagName"  v-else >
<el-input v-model="form.tagName" placeholder="请输入内容" ></el-input>
  </el-form-item>
</el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="closeDialog">取 消</el-button>
    <el-button type="primary" @click="changeDirectory">确 定</el-button>
  </span>
</el-dialog>
 </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
import { detail } from '@/api/hmmm/directorys.js'
import { detail as detailTag } from '@/api/hmmm/tags.js'
export default {

  name: 'commonAdd',
  props: {
    title: {
      type: String,
      default: '提示'
    }
  },
  data () {
    return {
      subjectList: [],
      dialogVisible: false,
      form: {
        id: null,
        subjectID: null,
        directoryName: '',
        tagName: ''
      },
      rules: {
        subjectID: [{ required: true, message: '请选择', trigger: 'blur' }],
        directoryName: [{ required: true, message: '请选择', trigger: 'blur' }],
        tagName: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      flag: true
    }
  },
  async created () {
    const { data } = await simple()
    this.subjectList = [...data]
  },
  methods: {
    async openDialog () {
      this.dialogVisible = true
    },
    closeDialog () {
      this.form = {
        id: null,
        subjectID: null,
        directoryName: ''
      }
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    async  changeDirectory () {
      try {
        await this.$refs.form.validate()
        this.$emit('changeDirectory', this.form)
        // this.closeDialog()
      } catch (error) {
        console.log(error)
      }
    },
    async geteditList (id) {
      if (this.flag) {
        const { data } = await detail({ id })
        this.form = data
        this.openDialog()
      } else {
        const { data } = await detailTag({ id })
        this.form = data
        this.openDialog()
      }
    }
  }
}
</script>

<style scoped lang='less'>
/deep/.el-input__inner{
  width: 300px;
}
</style>
